package DesignPatterns.builder;

/**
 * 建造者模式也是比较常用的模式了，常用的框架Lombok的注解@Builder就是将我们的实体类自动增加建造者。所以建造者就按字面意思理解即可，就是用来建造对象的模式。代码如下，核心是对外隐藏场景，只能有build方法来进行对象构建。
 * 优点：更加关注创建对象的过程，当参数过多时使用普通构造器创建很容易搞混参数，建造者不会。核心是利用静态内部类来对创建对象进行封装
 */
public class MyString {
    private String name;
    private String address;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    @Override
    public String toString() {
        return "MyString{" +
                "name='" + name + '\'' +
                ", address='" + address + '\'' +
                '}';
    }

    public void setAddress(String address) {
        this.address = address;
    }

    private MyString(String name, String address) {
        this.address = address;
        this.name = name;
    }

    public static MyStringBuilder builder() {
        return new MyStringBuilder();
    }

    public static class MyStringBuilder {
        private String name;
        private String address;

        public MyStringBuilder name(String name) {
            this.name = name;
            return this;
        }

        public MyStringBuilder address(String address) {
            this.address = address;
            return this;
        }

        public MyString build() {
            return new MyString(name, address);
        }
    }
}
